<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Wxpay;
use app\common\model\Car;
use app\common\model\cars\Order;
use app\common\model\Category;
use app\common\model\Recruitment;
use app\common\model\Truck;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $param = request()->param();
        extract($param);
        $car = new Car;
        $truck = new Truck;
        if (isset($keywords) && !$keywords) {
            $car->whereLike('title', '%' . $keywords . '%');
            $truck->whereLike('truck.title', '%' . $keywords . '%');
        }
        $cat->where('stauts', 'normal');
        $truck->where('truck.stauts', 'normal');
        $list['car_list'] = $car->paginate($size ?? 10);
        $list['truck_list'] = $truck->with(['shop'])->paginate($size ?? 10);
        $this->success('请求成功', $list);
    }

    public function category()
    {
        $list = Category::all(['type' => 'goodsCate', 'status' => 'normal']);
        $this->success('请求成功', $list);
    }
    /**
     * 二手车列表
     *
     */
    public function car_list()
    {
        $car = new Car;

        $param = request()->param();
        extract($param);
        if (isset($type) && $type) {
            $car->where('type', $type);
        }
        if (isset($keywords) && $keywords) {
            $car->whereLike('car.title', '%' . $keywords . '%');
        }
        $car->where('car.status', 'normal');
        $list = $car->with('shop')->paginate($size ?? 10);
        $this->success('请求成功', $list);
    }
    /**
     * 二手车详情
     *
     */
    public function car_info()
    {
        $param = request()->param();
        extract($param);
        $car = new Car;
        $info = $car->get($id, ['shop']);
        $this->success('请求成功', $info);
    }
    /**
     * 货车列表
     *
     */
    public function truck_list()
    {
        $param = request()->param();
        extract($param);
        $truck = new Truck;
        if (isset($keywords) && $keywords) {
            $truck->whereLike('truck.title', '%' . $keywords . '%');
        }
        $truck->where('truck.status', 'normal');
        $list = $truck->with(['shop'])->paginate($size ?? 10);
        $this->success('请求成功', $list);
    }
    /**
     * 货车详情
     *
     */
    public function truck_info()
    {
        $param = request()->param();
        extract($param);
        $car = new Truck;
        $info = $car->get($id, ['shop', 'huan']);
        $this->success('请求成功', $info);
    }
    /**
     * 预约车辆下单
     *
     */
    public function add_car_order()
    {
        $param = request()->param();
        extract($param);
        if ($type == 1) {
            $param['sn'] = sn('car_order', 'sn', 'CR');
            $car = Car::get($car_id);
            $param['title'] = $car->title;
            $param['image'] = $car->image;
            $param['pay_price'] = $car->paid_price;
        } else {
            $param['sn'] = sn('car_order', 'sn', 'TR');
            $truck = Truck::get($car_id);
            $param['title'] = $truck->title;
            $param['image'] = $truck->image;
            $param['pay_price'] = $truck->pay_price;
        }
        $param['user_id'] = $this->auth->id;
        Db::startTrans();
        try {
            $order = new Order();
            $order->save($param);
            queue('app\common\job\CarOrder', $order->id, 1800, 'carOrder1226');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error('下单失败：' . $e->getMessage());
        }
        $this->success('下单成功', $order->id);
    }
    /**
     * 立即付款
     *
     */
    public function pay()
    {
        if (!$this->request->isPost()) {
            $this->error('提交类型错误');
        }
        $post = $this->request->post();
        extract($post);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        Db::startTrans();
        try {
            $order = Order::get($id);
            if ($order['pay_price'] <= 0) {
                $order->save(['paytime' => time(), 'status' => '1']);
            } else {
                $userInfo = $this->auth->getUser();
                $sdk = Wxpay::wxpay($order->sn, $order['pay_price'], $userInfo['openid'], 'cart_order');
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error('提交支付失败：' . $e->getMessage());
        }
        $this->success('', $sdk ?? []);
    }
    /**
     * 我的约看
     *
     */
    public function looks()
    {
        $list = Order::with(['car'])->where('order.status', '1')->where('user_id', $this->auth->id)->order('id desc')->paginate();
        $this->success('', $list ?? []);
    }

    /**
     * 约看详情
     *
     */
    public function looksInfo()
    {
        $param = $this->request->param();
        extract($param);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        $data = Order::get(['id', $id], ['car']);
        $this->success('', $data ?? []);
    }
    /**
     * 招聘列表
     *
     */
    public function recruitment()
    {
        $param = $this->request->param();
        extract($param);
        $model = new Recruitment;
        if (isset($keywords) && $keywords) {
            $model->whereLike('title', '%' . $keywords . '%');
        }
        $list = $model->order('id desc')->paginate();
        $this->success('', $list ?? []);
    }
}
